package br.cin.ufpe.corba.namingservice;

import java.lang.reflect.Constructor;

import br.cin.ufpe.corba.ObjectRef;

public final class NamingContextHelper {
	
	public static NamingContextExt narrow(final Object obj)
	{
		if (obj == null)
		{
			return null;
		}
		else if (obj instanceof ObjectRef)
		{
			ObjectRef objectRef = (ObjectRef) obj;
		
			if (objectRef.is_a(NamingContextExt.class.getCanonicalName())) {
				try {
					Constructor<?> constructor = NamingContextStub.class.getConstructor(new Class[] {	ObjectRef.class });
					return (NamingContextExt)constructor.newInstance(new Object[] { objectRef });
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}

		throw new RuntimeException("Narrow failed");		
	}
}
